using System;
using System.Collections.Generic;
#if GODOT
using Godot;
#endif

namespace Common {
	public static class Directory {
#if GODOT
		static readonly Godot.Directory staticDirectory = new Godot.Directory();
#endif
		public static void Create(string path) {
#if STANDARD
			System.IO.Directory.CreateDirectory(path);
#endif
#if GODOT
			staticDirectory.MakeDirRecursive(path);
#endif
		}
		public static bool IsExists(string path) {
#if STANDARD
			return System.IO.Directory.Exists(path);
#endif
#if GODOT
			return staticDirectory.DirExists(path);
#endif
		}
		public static void Delete(string path) {
			if (!IsExists(path)) {
				return;
			}
			string[] files = GetFiles(path, true);
			foreach (string child in files) {
				File.Delete(child);
			}
			string[] directories = GetDirectories(path, true);
			foreach (string child in directories) {
				Delete(child);
			}
#if STANDARD
			System.IO.Directory.Delete(path);
#endif
#if GODOT
			staticDirectory.Remove(path);
#endif
		}

		public static string[] GetFiles(string path, bool showHidden = false) {
#if STANDARD
			return System.IO.Directory.GetFiles(path);
#endif
#if GODOT
			List<string> files = new List<string>();

			staticDirectory.ChangeDir(path);
			staticDirectory.ListDirBegin(true, !showHidden);
			while (true) {
				string child = staticDirectory.GetNext();
				if (!string.IsNullOrEmpty(child)) {
					if (File.IsExists(child)) {
						files.Add(path + "/" + child);
					}
				} else {
					staticDirectory.ListDirEnd();
					break;
				}
			}
			return files.ToArray();
#endif
		}
		public static string[] GetDirectories(string path, bool showHidden = false) {
#if STANDARD
			return System.IO.Directory.GetDirectories(path);
#endif
#if GODOT
			List<string> directories = new List<string>();

			staticDirectory.ChangeDir(path);
			staticDirectory.ListDirBegin(true, !showHidden);
			while (true) {
				string child = staticDirectory.GetNext();
				if (!string.IsNullOrEmpty(child)) {
					if (IsExists(child)) {
						directories.Add(path + "/" + child);
					}
				} else {
					staticDirectory.ListDirEnd();
					break;
				}
			}
			return directories.ToArray();
#endif
		}
	}
}